using HKB.Common;
using System;
using System.Data;
using System.Web;
public class AdminGroupBll
{
	private Conn conn = null;
	public AdminGroupBll()
	{
		this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
	}
	public AdminGroupBll(Conn _conn)
	{
		this.conn = _conn;
	}
	public bool AddAdminGroup(string AdminGroupName, int Access, string Remark, string Power, string UserIDs)
	{
		IDbDataParameter[] parameters = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@AdminGroupName", DbType.String, AdminGroupName),
			this.conn.CreateParameter("@Access", DbType.Int32, Access),
			this.conn.CreateParameter("@Remark", DbType.String, Remark),
			this.conn.CreateParameter("@Power", DbType.String, Power)
		};
		IDbDataParameter[] array = new IDbDataParameter[3];
		string[] array2 = UserIDs.Split(new char[]
		{
			','
		});
		string commandText = "Insert Into [HL_AdminGroup](AdminGroupName, Access, Remark, Power) values(@AdminGroupName, @Access, @Remark, @Power)";
		string commandText2 = "Insert Into [HL_AdminUserGroup](AdminID,GroupID,Remark) values(@AdminID,@GroupID,@Remark)";
		this.conn.BeginTransaction();
		bool result;
		try
		{
			this.conn.ExecuteTransaction(commandText, parameters);
			int num = Convert.ToInt32(this.conn.ExecuteTransactionScalar("Select Top 1 AdminGourpID From HL_AdminGroup Order By AdminGourpID Desc"));
			for (int i = 0; i < array2.Length; i++)
			{
				int num2 = Text.ToInt32(array2[i]);
				if (num2 > 0)
				{
					array[0] = this.conn.CreateParameter("@AdminID", DbType.Int32, num2);
					array[1] = this.conn.CreateParameter("@GroupID", DbType.Int32, num);
					array[2] = this.conn.CreateParameter("@Remark", DbType.String, "0");
					this.conn.ExecuteTransaction(commandText2, array);
				}
			}
		}
		catch
		{
			this.conn.Rollback();
			result = false;
			return result;
		}
		this.conn.Commit();
		result = true;
		return result;
	}
	public bool UpdateGroup(string AdminGroupName, int Access, string Remark, string Power, int AdminGroupID, string UserIDs)
	{
		IDbDataParameter[] parameters = new IDbDataParameter[]
		{
			this.conn.CreateParameter("@AdminGroupName", DbType.String, AdminGroupName),
			this.conn.CreateParameter("@Access", DbType.Int32, Access),
			this.conn.CreateParameter("@Remark", DbType.String, Remark),
			this.conn.CreateParameter("@Power", DbType.String, Power),
			this.conn.CreateParameter("@AdminGroupID", DbType.Int32, AdminGroupID)
		};
		IDbDataParameter[] array = new IDbDataParameter[3];
		string[] array2 = UserIDs.Split(new char[]
		{
			','
		});
		string commandText = "Update [HL_AdminGroup] Set AdminGroupName=@AdminGroupName, Access=@Access, Remark=@Remark, Power=@Power Where AdminGourpID=@AdminGroupID";
		string commandText2 = "Insert Into [HL_AdminUserGroup](AdminID,GroupID,Remark) values(@AdminID,@GroupID,@Remark)";
		this.conn.BeginTransaction();
		bool result;
		try
		{
			this.conn.ExecuteTransaction(commandText, parameters);
			string arg = "";
			if (array2.Length > 0 && !string.IsNullOrEmpty(UserIDs))
			{
				arg = " and AdminID not in(" + UserIDs + ") ";
			}
			if (Convert.ToInt32(this.conn.ExecuteTransactionScalar("select count(*) from [HL_AdminUserGroup] where GroupID=" + AdminGroupID + arg)) > 0)
			{
				this.conn.ExecuteTransaction("Delete From HL_AdminUserGroup Where GroupID=" + AdminGroupID + arg);
			}
			for (int i = 0; i < array2.Length; i++)
			{
				int num = Text.ToInt32(array2[i]);
				if (num > 0)
				{
					if (Convert.ToInt32(this.conn.ExecuteTransactionScalar(string.Concat(new object[]
					{
						"select count(*) From HL_AdminUserGroup Where AdminID=",
						num,
						" and GroupID=",
						AdminGroupID
					}))) == 0)
					{
						array[0] = this.conn.CreateParameter("@AdminID", DbType.Int32, num);
						array[1] = this.conn.CreateParameter("@GroupID", DbType.Int32, AdminGroupID);
						array[2] = this.conn.CreateParameter("@Remark", DbType.String, "0");
						this.conn.ExecuteTransaction(commandText2, array);
					}
				}
			}
		}
		catch (Exception ex)
		{
			this.conn.Rollback();
			ex.WriteErrorLog(new string[0]);
			result = false;
			return result;
		}
		this.conn.Commit();
		result = true;
		return result;
	}
	public int GetAdminNumByGroupID(int GroupID)
	{
		return (int)this.conn.GetScalar("Select count(AdminID) From [HL_AdminUserGroup] Where GroupID=" + GroupID, new IDbDataParameter[0]);
	}
	public DataTable GetAdminGroupByAdminID(int AdminID)
	{
		return this.conn.GetDataTable("Select * From [HL_AdminGroup] Where AdminGourpID in(Select GroupID From [HL_AdminUserGroup] Where AdminID=" + AdminID + ")");
	}
	public DataTable GetAllAdminGroup()
	{
		return this.conn.GetDataTable("Select *,(Select count(AdminID) From [HL_AdminUserGroup] Where HL_AdminUserGroup.GroupID=HL_AdminGroup.AdminGourpID) as AdminNum From [HL_AdminGroup] Order By AdminGourpID Desc");
	}
	public DataTable GetAdminGroupByID(int AdminGourpID)
	{
		return this.conn.GetDataTable("Select * From [HL_AdminGroup] Where AdminGourpID=" + AdminGourpID);
	}
	public bool DeleteGroup(int AdminGroupID)
	{
		return this.conn.Execute("DELETE From [HL_AdminGroup] Where AdminGourpID=" + AdminGroupID) == 1;
	}
}
